<template>
	<custom-layout custom-class="custom-layout">
		<!-- 表单 -->
		<wd-form ref="formRef" :model="formData">
			
			<wd-cell-group border>

				<wd-datetime-picker 
				  type="date" 
				  v-model="formData.startTime" 
				  prop="startTime" 
				  :rules="[
				    { required: true, message: '请选择开工时间' },
				    {
				      validator: (value) => {
				        if (!formData.endTime) return true;
				        return dayjs(value).isBefore(formData.endTime);
				      },
				      message: '开工时间不能大于完工时间'
				    }
				  ]" 
				  label="开工时间" 
				  @confirm="handleConfirm" 
				/>
				
				<wd-datetime-picker 
				  type="date" 
				  v-model="formData.endTime" 
				  prop="endTime" 
				  :rules="[
				    { required: true, message: '请选择完工时间' },
				    {
				      validator: (value) => {
				        if (!formData.startTime) return true;
				        return dayjs(value).isAfter(formData.startTime);
				      },
				      message: '完工时间不能小于开工时间'
				    }
				  ]" 
				  label="完工时间" 
				  @confirm="handleConfirm" 
				/>
				
			</wd-cell-group>
		</wd-form>
		<!-- 提交 -->
		<wd-button block size="large" custom-class="submit-button" @click="handleSubmit">提交</wd-button>
		
	</custom-layout>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useDictionaryStore,
		useUserStore
	} from '@/store/index.js'
	import {
		engineeringStageConfirmComplete,
		updateEngineeringStage
	} from '@/api/engineering/detail.js'
	import dayjs from 'dayjs'

	// 字典仓库
	const dictionaryStore = useDictionaryStore()
	// 用户仓库
	const userStore = useUserStore()
	
	// 表单ref
	const formRef = ref()
	// 表单数据
	const formData = reactive({
		startTime:'',
		endTime:''
	})
	const otherDate = ref({})

	onLoad((option)=>{
		formData.startTime=JSON.parse(decodeURIComponent(option.row)).startTime
		formData.endTime=JSON.parse(decodeURIComponent(option.row)).endTime
		otherDate.value = JSON.parse(decodeURIComponent(option.row))
	})
	// 处理提交
	const handleSubmit = async () => {
		const validateResult = await formRef.value.validate()
		if (!validateResult.valid) return
		uni.showLoading({
			mask: true
		})
		const params = {
			...otherDate.value,
			...formData,
			
		}
		console.log(params,'params');
		
		updateEngineeringStage(params).then(()=>{
			uni.navigateBack(); // 返回上一页
			uni.$emit('refresh-confirm-complete'); // 触发刷新线索列表
		}).catch((error) => {
			console.log(error);
			const title= error
			setTimeout(() => {
				uni.showToast({
					title: title,
					icon: 'error'
				})
			})
		}).finally(() => {
			uni.hideLoading()
			
		})
		
	}
	const handleConfirm = ({ value, prop }) => {
  if (value) {
    formData[prop] = dayjs(value).format('YYYY-MM-DD 23:59:59');
  } else {
    formData[prop] = undefined;
  }
};
	
</script>

<style lang="scss" scoped>
	// 自定义布局
	:deep(.custom-layout) {
		background: #F8F8F8;
	}

	// 提交按钮
	:deep(.submit-button) {
		margin: 32rpx;
	}
	.cell-title {
		font-size: 30rpx;
		font-weight: bold;
		margin: 24rpx;
		padding-left: 10rpx;
		position: relative;
	}
	.cell-title:before {
		content: "";
		position: absolute;
		left: -2px;
		top: 15%;
		height: 70%;
		width: 6rpx;
		background-color: #6a9cff;
	}
</style>